

#include "ImageRecorder.hpp"
namespace Moduels::FramesIO
{

    void ImageRecorder::Init()
    {
        Path=CreateFilePath(Path+"/image");
    }

    std::string ImageRecorder::CreateFilePath(std::string path)
    {
        int index=0;
        if(!boost::filesystem::exists(path))
        {
            if (!boost::filesystem::create_directories(path))
            {
                std::cout << "Create Image Path:" << path << " Failed" << std::endl;
            }
        }
        for(auto iterator=begin(boost::filesystem::directory_iterator(path));
            iterator!=end(boost::filesystem::directory_iterator(path));iterator++)
        {
            if(is_directory((*iterator)))
            {
                index++;
//                std::cout<<(*iterator).path().filename()<<std::endl;
            }

        }
        index++;
        path=path+"/"+std::to_string(index);
        if(!boost::filesystem::exists(path))
        {
            if (!boost::filesystem::create_directories(path))
            {
                std::cout << "Create Image Child Path:" << path << " Failed" << std::endl;
            }
        }
        return path;
    }

    void ImageRecorder::AddFrameToImage(cv::Mat frame)
    {
        std::string path=Path+"/"+std::to_string(Index)+Format.ImageSuffix;
        cv::imwrite(path,frame);
        Index++;
    }
}